package utils

import (
	"encoding/binary"
	"encoding/hex"
	"math/rand"
)

func Murmurhash2(key []byte, seed uint32) uint32 {

	m := uint32(0x5bd1e995)

	r := 24

	keyLen := uint32(len(key))

	h := seed ^ keyLen

	offset := 0

	e := binary.LittleEndian

	for keyLen >= 4 {

		k := e.Uint32(key[offset : offset+4])

		k *= m

		k ^= k >> r

		k *= m

		h *= m

		h ^= k

		offset += 4

		keyLen -= 4

	}

	switch keyLen {

	case 3:

		h ^= uint32(key[offset+2]) << 16

		fallthrough

	case 2:

		h ^= uint32(key[offset+1]) << 8

		fallthrough

	case 1:

		h ^= uint32(key[offset])

		h *= m

	}

	h ^= h >> 13

	h *= m

	h ^= h >> 15

	return h

}

func GenerateRandomString(length int) (string, error) {
	bytes := make([]byte, length)
	_, err := rand.Read(bytes)
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(bytes), nil
}
